home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8086 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  78 lines

  1. Path: flute.aix.calpoly.edu!not-for-mail
  2. From: mporcell@flute.aix.calpoly.edu (Michael Anthony Porcelli)
  3. Newsgroups: comp.lang.c++
  4. Subject: Bizzare C++ bug...PLEASE CHECK IT OUT
  5. Date: 14 Feb 1996 05:25:29 -0800
  6. Organization: California Polytechnic State University, San Luis Obispo
  7. Message-ID: <4fsns9$8ga3@flute.aix.calpoly.edu>
  8. NNTP-Posting-User: mporcell@flute.aix.calpoly.edu
  9.  
  10. Hello all you helpful people.  I was working on a project for school and I
  11. ran into a super hard-to-find bug.  Well, I found it and the project is
  12. complete, but *where* the bug occured was what made it so hard to find and I
  13. *still* don't understand why it occured when it did.  Any input on this
  14. would be appreciated.  Thanks.  Here it is:
  15.  
  16. struct A {
  17.    A(...);
  18.    ...
  19.    virtual ostream &Print(void) = 0;   // r1
  20.    friend ostream &operator<<(ostream &os, const A &a) {return l.Print(os)}
  21.      //r2
  22.    ...
  23. }
  24.  
  25. struct B : public A {
  26.    B(...);
  27.    ...
  28. }
  29.  
  30. B::B(...) : A(...), ... {  //r3
  31.    //              ^
  32.    // Bug *occurs* here
  33.    ...
  34.    os << *this;  //r4
  35.    // ^^
  36.    // Bug *is* here
  37.    ...
  38. }
  39.    
  40. struct C : public B {
  41.    ...
  42.    ostream &Print(void) const;  //r5
  43.    ...
  44. }
  45. ...
  46. ostram &C::Print(void) {
  47.    ...
  48. }
  49.  
  50. Well.  I've tried to show in as much detail as I could exactly what was
  51. going on.  The bug was a compile time bug that produced the message:
  52.  
  53. Abort Process
  54.  
  55. Clearly the call to the overloaded operator<< (r2) at r4 will cause a crash
  56. because it is a call from a base class constructor (B) to a virtual print 
  57. function (r1) that does not get defined until the derived class C (r5).  The 
  58. reason that this bug was so darn hard to find is that it occured at r3 and 
  59. *not* at its location in the code (r4).  I still have absolutely *no* idea
  60. why this is so.  It is perhaps a compiler dependant thing.  If some of you
  61. could compile and run my above code, maybe you could see if it occurs at the 
  62. same point in the code as it did for me.  I am absolutely certain that it was
  63. occuring where I indicated it was occuring (r3) because I spent hours on end
  64. isolating it down to that exact spot.  Any insight would be appreciated.
  65. I'm using xlC on IBM AIX.  I've also created a super simpified model of my 
  66. project with actual filled in C++ code that I could email you if you'd like 
  67. to take a look at it further.
  68.  
  69. You might be looking at this program that I wrote and be wondering why the
  70. hell I'm doing this pure virtual print function and calling it in the base
  71. class friend operator<<  This is actually a very useful technique for
  72. creating what amounts to a "virtual operator <<"
  73.  
  74. Thanks in advance,
  75.  
  76. -Mike
  77.  
  78.